from pyecharts.charts import Radar, Page
import pyecharts.options as op
import random
import pandas as pd
import numpy as np
from pyecharts.charts import WordCloud, Page
from pyecharts import options as opts
from collections import Counter
from pyecharts import options
import os
flag = 0
def getRadar(key_tag,I):
    pic_list = []
    df_data = pd.read_csv('up.csv')
    df_data_1 = pd.read_csv('data.csv')
    df_data_1.dropna(inplace=True)

    df_data.drop_duplicates(subset=None, keep='first', inplace=True)
    df_data_1.drop_duplicates(subset=['video_title', 'video_up_mid'], inplace=True)
    # 去重↑
    df_data_0 = pd.merge(df_data, df_data_1, left_on='mid', right_on='video_up_mid', how='left')
    df_data_0.dropna(inplace=True)
    df_data1 = df_data_0[
        ['video_up_mid', 'video_title', 'video_play', 'video_created', 'video_like', 'video_coin', 'video_fav',
         'video_reply', 'video_share', 'video_danmaku', 'video_tags']]
    # 合并表 df_data_0：能对应上up主的视频
    df_data1_up = df_data1
    df_data_0_copy = df_data_0.copy()
    data_ = pd.DataFrame(df_data_0_copy['video_tags'])
    df_data_0_copy = df_data_0_copy[(data_['video_tags'].str.contains(key_tag))]
    df_data_0_copy.drop_duplicates(subset=['video_tags'], inplace=True)

    data1 = df_data_0_copy[['video_up_mid', 'video_title', 'video_like', 'video_coin', 'video_fav', 'video_share']]
    df_data1_mid = df_data1['video_up_mid']
    indexs = data1._stat_axis.values.tolist()
    Num = []
    for index in indexs:
        Num.append(index)
    indexs2 = df_data1._stat_axis.values.tolist()
    N = []
    for index in indexs2:
        N.append(index)

    def getMax(dict_list):
        max_value = max(dict_list)
        return max_value

    def getName(c):
        d_name = pd.DataFrame(columns=['up_name'])
        d_name.drop(d_name.index, inplace=True)
        d_name = d_name.drop(index=d_name.index)
        d_mid = pd.DataFrame(columns=['mid'])
        d_mid.drop(d_mid.index, inplace=True)
        d_mid = d_mid.drop(index=d_mid.index)
        d_name = df_data[['up_name']]
        d_mid = df_data[['mid']]
        for ino in range(20):
            if (d_mid.loc[ino].values.tolist()[0] == c):
                return str(d_name.loc[ino].values.tolist()[0])

    colors = ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570',
              '#c4ccd3', '#f05b72', '#ef5b9c', '#f47920', '#905a3d', '#fab27b', '#2a5caa', '#444693', '#726930',
              '#b2d235', '#6d8346', '#ac6767', '#1d953f', '#6950a1', '#918597']
    for i in I:
        page = Page()
        result_format = pd.DataFrame(columns=['video_tags'])
        result_format.drop(result_format.index, inplace=True)
        result_format = result_format.drop(index=result_format.index)
        final_data = pd.DataFrame(
            columns=['video_up_mid', 'video_title', 'video_like', 'video_coin', 'video_fav', 'video_share'])
        final_data.drop(final_data.index, inplace=True)
        final_data = final_data.drop(index=final_data.index)
        for num in Num:
            if (int(df_data1_mid.loc[num]) == i):
                final_data.loc[num] = data1.loc[num]
        video_title = final_data[['video_title']]
        video_like = final_data[['video_like']].values
        video_coin = final_data[['video_coin']].values
        video_fav = final_data[['video_fav']].values
        video_share = final_data[['video_share']].values
        final_data_1 = final_data[['video_like', 'video_coin', 'video_fav', 'video_share']]
        indexs3 = final_data_1._stat_axis.values.tolist()
        f_d = final_data_1.stack()
        f_d = f_d.unstack(0)
        x_schema = [
            {"name": "点赞", "max": getMax(video_like) * 1.2, "min": 0, "color": 'black', "font_size": 18},
            {"name": "投币", "max": getMax(video_coin) * 1.2, "min": 0, "color": 'black', "font_size": 18},
            {"name": "收藏", "max": getMax(video_fav) * 1.2, "min": 0, "color": 'black', "font_size": 18},
            {"name": "转发", "max": getMax(video_share) * 1.2, "min": 0, "color": 'black', "font_size": 18}
        ]

        radar_x = Radar()
        radar_x.add_schema(x_schema, splitarea_opt=opts.SplitAreaOpts(
            is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
        ), )
        up_mean = [
            [int(df_data1['video_like'].mean()), int(df_data1['video_coin'].mean()), int(df_data1['video_fav'].mean()),
             int(df_data1['video_share'].mean())]]
        radar_x.add("up主视频平均情况", up_mean, color=random.choice(colors)).set_colors(
            [random.choice(colors)])
        for index in indexs3:
            video_d = f_d[[index]]
            video = video_d.values
            video_li = video.tolist()
            Matrix = [[video_li[imm] for imm in range(4)] for j in range(len(indexs3))]
            radar_x.add(str(video_title.loc[index].values), Matrix, color=random.choice(colors)).set_colors(
                [random.choice(colors)])
        radar_x.set_global_opts(
            title_opts=op.TitleOpts(title="up" + str(getName(i)) + "雷达图", pos_right="center"),
            legend_opts=op.LegendOpts(legend_icon="roundRect", align="left", pos_left='77%',
                                      pos_bottom='10%', orient='vertical', type_='scroll')
        )
        pic_list.append(radar_x)
        # df_data1 清洗后的可用数据
        for n1 in N:
            if (int(df_data1_mid.loc[n1]) == i):
                result_format.loc[n1] = df_data1.loc[n1]
        result_format_tags = result_format[["video_tags"]]
        string_data = ','.join(list(result_format_tags['video_tags'].values))
        string_data_1 = string_data.replace("[", "")
        string_data_2 = string_data_1.replace("]", "")
        string_data_3 = string_data_2.replace(" ", "")
        data = string_data_3.split(",")
        counter = Counter(data)
        c = counter.items()
        w = (
            WordCloud()
                .add("", c, word_size_range=[10, 70])
                .set_global_opts(
                title_opts=options.TitleOpts(title="up" + str(getName(i)) + "视频tag词云图", pos_right="center"))
        )
        pic_list.append(w)
    return pic_list